package com.model.dao;

//Generated Sep 14, 2013 1:19:21 AM by Hibernate Tools 4.0.0

import java.util.List;

import javax.ejb.Stateless;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.dto.merchant.UserDTO;

/**
 * Home object for domain model class UserTable.
 * 
 * @see com.model.dao.com.model.dao.UserTable
 * @author Hibernate Tools
 */
@Stateless
public class UserTableDAO {

	private static final Log log = LogFactory.getLog(UserTableHome.class);
	Session session;
	Transaction tx;

	// TODO implement Exceptions
	public Integer persist(UserTable transientInstance) throws Exception {
		log.debug("persisting UserTable instance");
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			Integer userId=(Integer) session.save(transientInstance);
			tx.commit();
			log.debug("persist successful");
			return userId;
		} catch (Exception ex) {
			session.close();
			if (tx != null) {
				tx.rollback();
			}
			log.error("persist failed", ex);
			throw ex;
		}
	}

	public void remove(UserTable persistentInstance) throws Exception {
		log.debug("removing UserTable instance");
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			session.delete(persistentInstance);
			tx.commit();
			log.debug("remove successful");
		} catch (Exception ex) {
			session.close();
			if (tx != null) {
				tx.rollback();
			}
			log.error("remove failed", ex);
			throw ex;
		}
	}

	public UserTable merge(UserTable detachedInstance) throws Exception {
		log.debug("merging UserTable instance");
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			UserTable result = (UserTable) session.merge(detachedInstance);
			tx.commit();
			log.debug("merge successful");
			return result;
		} catch (Exception ex) {
			session.close();
			if (tx != null) {
				tx.rollback();
			}
			log.error("merge failed", ex);
			throw ex;
		}
	}

	public UserTable findById(int id) throws Exception {
		log.debug("getting UserTable instance with id: " + id);
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			UserTable instance = (UserTable) session.get(UserTable.class, id);
			tx.commit();
			log.debug("get successful");
			return instance;
		} catch (Exception ex) {
			session.close();
			if (tx != null) {
				tx.rollback();
			}
			log.error("get failed", ex);
			throw ex;
		}
	}
	@SuppressWarnings("unchecked")
	public List<MastercatalogTable> finadAll(){
		log.debug("getting All MastercatalogTable instances ");
		try {
			Session session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			Transaction tx = session.beginTransaction();
			Query query = session.createQuery("from MastercatalogTable");
			List<MastercatalogTable> list = query.list();
			tx.commit();
			log.debug("get successful");
			return list;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	@SuppressWarnings("unchecked")
	public UserTable findByUserName(String mobileno) throws Exception {
		log.debug("getting UserTable instance with id: " + mobileno);
		UserTable table=null;
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			Query query = session.createQuery("from UserTable where mobileno="+mobileno);
			List<UserTable> list = query.list();
			tx.commit();
			log.debug("get successful");
			if(null!=list && list.size()>0){
			 table=(UserTable) list.get(0);
			}
		} catch (Exception ex) {
			if(null!=session && session.isOpen())
			session.close();
			if (tx != null && tx.isActive()) {
				tx.rollback();
			}
			log.error("get failed", ex);
			throw ex;
		}
		return table;
	}
	public static void main(String[] args) throws Exception {
		UserTableDAO dao = new UserTableDAO();
//		UserTable table = dao.findById(1);
		UserTable table = dao.findByUserName("996618358");
//		UserDTO dto=new UserDTO();
//		dto.setStreet("street");
//		dto.setArea("Madina guda");
//		dto.setLandmark("Opp Reliance Digital");
//		dto.setHouseno("Flat no 402 Raghava Towers");
//		dto.setIduserTable(1);
//		dao.updateUser(dto);
//		System.out.println(table.getMobileno());
	}
	
	public void updateUser(UserDTO dto) throws Exception {
		log.debug("getting UserTable instance with id: " + dto.getMobileno());
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			Query query = session
					.createQuery("update UserTable set area = :area"
								+", houseno = :houseno"
								+", street = :street"
								+", landmark = :landmark"
								+ " where iduserTable= :id");
			query.setParameter("area", dto.getArea());
			query.setParameter("houseno", dto.getHouseno());
			query.setParameter("street", dto.getStreet());
			query.setParameter("landmark", dto.getLandmark());
			query.setParameter("id", dto.getIduserTable());
			Integer id = query.executeUpdate();
			tx.commit();
			log.debug("get successful");
		} catch (Exception ex) {
			session.close();
//			if (tx != null) {
//				tx.rollback();
//			}
			log.error("get failed", ex);
			throw ex;
		}
	}
	
	// mysqlconnector
	// slf4j dom4j jbossloggin javaassist hibernate-commons-annotations
	// hibernate-core, jpa antlr
}
